/**
 * 获取店铺激活码接口
 */

//引入Q模块
let Q = require('q');
//引入公共函数
let util = require('../../util');

//getRegisterCode接口入口函数
let index = function(getData, postData, config, redis, db) {
	let deferred = Q.defer();
	if(postData.storeName && postData.session) {
		let registerCode = '';
		util.checkAdminLogin(redis, postData.session).then(function(loginData) {
			return util.createRandomStr(6);
		}, deferred.reject).then(function(_registerCode) {
			registerCode = _registerCode;
			return redis.set('registerCode-' + registerCode, JSON.stringify({
				storeName: postData.storeName
			}));
		}, function() {
			//生成商户激活码失败
			deferred.reject('-0192');
		}).then(function() {
			deferred.resolve(registerCode);
		}, function() {
			//存储商户激活码失败
			deferred.reject('-0193');
		});
	}
	else {
		//获取商家激活码接口参数不足
		deferred.reject('-0194');
	}
	return deferred.promise;
}

module.exports = {
	index: index
}